package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.JuheProvinceMapper;
import com.zjdiepu.www.ibiz.third.IJuheProvinceService;
import com.zjdiepu.www.model.third.dto.JuheProvince;
import com.zjdiepu.www.model.third.ext.JuheProvinceExt;

/**
 * 省份Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年07月18日 18时03分<br/>
 * @since 1.0<br/>
 */
@Service
public class JuheProvinceServiceImpl extends BaseService implements IJuheProvinceService {
	public static final Logger logger = LoggerFactory.getLogger(JuheProvinceServiceImpl.class);

	@Autowired
	private JuheProvinceMapper juheProvinceMapper;
	
	@Override
    public String getNameById(String proId) {
        if(StringUtils.isNotBlank(proId)) {
            JuheProvince juheProvince = new JuheProvince();
            juheProvince.setProId(proId);
            juheProvince.setDpStatus(DpStatus.NORMAL.getCode());
            juheProvince = juheProvinceMapper.selectRefined(juheProvince);
            if(null != juheProvince) {
                return juheProvince.getProName();
            }
        }
        return null;
    }
	
	@Override
	public void saveUpdateAndDelete(JuheProvince juheProvince, OperateType operType) {
		switch (operType) {
		case add:
			juheProvince.initInsertData(juheProvince.getModifier(), juheProvince.getModifierName());
			juheProvinceMapper.insert(juheProvince);
			break;
		case edit:
			juheProvince.initUpdateData(juheProvince.getModifier(), juheProvince.getModifierName());
			// 影响的记录数
			int updateCount = juheProvinceMapper.updateByPrimaryKeySelective(juheProvince);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			juheProvince.initDeleteData(juheProvince.getModifier(), juheProvince.getModifierName());
			// 影响的记录数
			int deleteCount = juheProvinceMapper.updateByPrimaryKeySelective(juheProvince);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<JuheProvince> selectByExt(JuheProvinceExt juheProvinceExt) {
		return juheProvinceMapper.selectByExt(juheProvinceExt);
	}
	
	@Override
	public int countByExt(JuheProvinceExt juheProvinceExt){
		return juheProvinceMapper.countByExt(juheProvinceExt);
	}
	
	@Override
	public JuheProvince selectById(String id) {
		return juheProvinceMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			juheProvinceMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(JuheProvince juheProvince) {
		return juheProvinceMapper.countRefined(juheProvince);
	}

	@Override
	public JuheProvince selectRefined(JuheProvince juheProvince) {
		return juheProvinceMapper.selectRefined(juheProvince);
	}
	
	@Override
	public int deletedRefined(JuheProvince juheProvince) {
		return juheProvinceMapper.deletedRefined(juheProvince);
	}

	@Override
	public boolean repeat(JuheProvince juheProvince) {
		int count = countRefined(juheProvince);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(juheProvince.getId())) {
			return true;
		}
		JuheProvince dbItem = selectRefined(juheProvince);
		if(null == dbItem) {
			return false;
		}
		return !juheProvince.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return juheProvinceMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<JuheProvince> juheProvinces) {
		return juheProvinceMapper.insertBatch(juheProvinces);
	}
}
